import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-09-02
 * Time: 21:41
 */
public class Test {
    //leetcode 495 提莫攻击
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int n = timeSeries.length;
        int ret = 0;
        for(int i = 0; i < n-1; i++) {
            if(timeSeries[i + 1] - timeSeries[i] <= duration) ret += timeSeries[i + 1] - timeSeries[i];
            else ret += duration;
        }

        return ret += duration;
    }

    //leetcode 6 N字形变换
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int d = 2 * numRows - 2;
        int n = s.length();
        StringBuilder ret = new StringBuilder();
        for(int i = 0; i < n; i += d) {
            ret.append(s.charAt(i));
        }
        for(int i = 1; i < numRows - 1; i++) {
            for(int j = i, k = d - j; j < n || k < n; j += d,k += d) {
                if(j < n) ret.append(s.charAt(j));
                if(k < n) ret.append(s.charAt(k));
            }
        }
        for(int i = numRows - 1; i < n; i += d) {
            ret.append(s.charAt(i));
        }

        return ret.toString();
    }

    //leetcode 38 外观数列
    public String countAndSay(int n) {
        String ret = "1";
        for(int i = 1; i < n; i++) {
            int left = 0, right = 0;
            StringBuilder tmp = new StringBuilder();
            int len = ret.length();
            while(right < len) {
                while(right < len && ret.charAt(left) == ret.charAt(right)) right++;
                tmp.append(Integer.toString(right - left));
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();
        }
        return ret;
    }

    public int minNumberOfFrogs(String croakOfFrogs) {
        String s = "croak";
        int n = s.length();
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0; i < n; i++) {
            map.put(s.charAt(i),i);
        }
        int[] hash = new int[n];
        for(int i = 0; i < croakOfFrogs.length(); i++) {
            char ch = croakOfFrogs.charAt(i);
            if(map.get(ch) != 0) {
                if(hash[map.get(ch) - 1] == 0) return -1;
                hash[map.get(ch) - 1]--;
                hash[map.get(ch)]++;
            }else {
                if(hash[n - 1] != 0) {
                    hash[n - 1]--;
                    hash[0]++;
                }else {
                    hash[0]++;
                }
            }
        }
        for(int i = 0; i < n -1; i++) {
            if(hash[i] != 0) return -1;
        }

        return hash[n-1];

    }
}
